const isEncoded = (uri: string) => {
  uri = uri || '';
  try {
    return uri !== decodeURIComponent(uri);
  } catch (error) { // JM：未编码的URI中包含%，就会发生uri malformed的问题
    console.warn(`uri malformed error[Ignored] ${error}`);
    return false;
  }
}

const encodeURIWrapper = (url: string) => {
  if (isEncoded(url)) {
    return url;
  } else {
    url = encodeURIComponent(url.trim());
    return url.replaceAll("(", "%28").replaceAll(")", "%29").replaceAll("_", "%5F");
  }
}

const decodeURIWrapper = (url: string) => {
  url = decodeURIComponent(url);
  return url.replaceAll("%28", "(").replaceAll("%29", ")").replaceAll("%5F", "_");
}

export {
  encodeURIWrapper,
  decodeURIWrapper,
}
